import TencentCloudChat from '@tencentcloud/chat';
import liveApi from './apiList/liveApi';
import TIMUploadPlugin from 'tim-upload-plugin';
let IM = null //信息本体
let chat = null
let params = {
	sdkAppId: 1400752704, // Please replace with your own sdkAppId
	userId: `${uni.getStorageSync('user').id}`, //用户 ID
	userSig: "", // Please replace with your own userSig
}
// 获取用户签名
export const getuserSig = () => {
	liveApi.membergetLiveTokens({
		id: params.userId
	}).then(res => {
		params.userSig = res.data.code
		startChat()
	})
}
const login = () => {
	return chat.login({
		userID: params.userId,
		userSig: params.userSig,
	})
}
// 初始化&登录
const startChat = async () => {
	chat = TencentCloudChat.create({
		SDKAppID: params.sdkAppId
	});
	// chat.setLogLevel(0)
	chat.registerPlugin({
		'tim-upload-plugin': TIMUploadPlugin
	});
	if (chat.getLoginUser() == '') {
		const res = await login()
	}
	if (chat.isReady()) {
		chaton()
	} else {
		chat.on(TencentCloudChat.EVENT.SDK_READY, chaton);
	}
}
const chaton = (event) => {
	console.log('初始化成功，开始监听', event);
	// 监听单聊、群聊、群提示、群系统通知的新消息
	chat.on(TencentCloudChat.EVENT.MESSAGE_RECEIVED, getMsg);

}
const getMsg = (e) => {
	console.log('监听', e);
}